package com.yycx.module.user.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.query.CriteriaDelete;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.module.user.client.entity.SysPoster;
import com.yycx.module.user.provider.mapper.SysPosterMapper;
import com.yycx.module.user.provider.service.SysPosterService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import org.springframework.stereotype.Service;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.query.CriteriaQuery;
import org.springframework.transaction.annotation.Propagation;
import com.yycx.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分享海报接口实现类
 *
 * @author flyme
 * @date 2023-06-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysPosterServiceImpl extends BaseServiceImpl<SysPosterMapper, SysPoster> implements SysPosterService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, SysPoster poster, EntityMap extra) {
        return ResultBody.ok();
    }

    @Override
    public ResultBody afterEdit(CriteriaUpdate cu, SysPoster sysPoster, EntityMap extra) {
        try {
            Set<String> keys = redisUtils.keys(CommonConstants.POSTER_KEY + "*");
            if (CollUtil.isNotEmpty(keys)) {
                redisUtils.delArr(keys.stream().collect(Collectors.toList()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.afterEdit(cu, sysPoster, extra);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<SysPoster> cq, SysPoster poster, EntityMap requestMap) {
        cq.orderByDesc("poster.createTime");
        return ResultBody.ok();
    }
}
